package com.hunttown.mes.convert;

import com.google.common.collect.Lists;
import com.hunttown.mes.common.utils.BeanUtils;
import com.hunttown.mes.domain.AnalysisVisualCatalog;
import com.hunttown.mes.rpc.domain.AnalysisVisualCatalogDTO;
import com.hunttown.mes.rpc.exception.ConvertException;
import com.hunttown.mes.rpc.domain.enums.ErrorCode;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;

/**
 * created by wangjunfu on 2018-12-12 18:11:35
 */
public class AnalysisVisualCatalogConverts {

    private static Logger LOG = LoggerFactory.getLogger(AnalysisVisualCatalogConverts.class);

    public static List<AnalysisVisualCatalogDTO> convert(List<AnalysisVisualCatalog> analysisVisualCatalogList) {
        if (CollectionUtils.isEmpty(analysisVisualCatalogList)) {
            return Collections.EMPTY_LIST;
        }

        List<AnalysisVisualCatalogDTO> analysisVisualCatalogListResult = Lists.newArrayList();
        for (AnalysisVisualCatalog analysisVisualCatalog : analysisVisualCatalogList) {
            analysisVisualCatalogListResult.add(convert(analysisVisualCatalog));
        }
        return analysisVisualCatalogListResult;
    }

    public static AnalysisVisualCatalogDTO convert(AnalysisVisualCatalog analysisVisualCatalog) {
        try {
            return BeanUtils.copy(analysisVisualCatalog, AnalysisVisualCatalogDTO.class);
        } catch (Exception e) {
            LOG.error("AnalysisVisualCatalog到AnalysisVisualCatalogDTO转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "AnalysisVisualCatalog到AnalysisVisualCatalogDTO转换失败", e);
        }
    }

    public static AnalysisVisualCatalog convert(AnalysisVisualCatalogDTO analysisVisualCatalogDTO) {
        try {
            return BeanUtils.copy(analysisVisualCatalogDTO, AnalysisVisualCatalog.class);
        } catch (Exception e) {
            LOG.error("AnalysisVisualCatalogDTO到AnalysisVisualCatalog转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "AnalysisVisualCatalogDTO到AnalysisVisualCatalog转换失败", e);
        }
    }
}